package com.damie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dadie.Domain.ShopDomain;
import com.dadie.Domain.ShopOrderDomain;
import com.dadie.Domain.ShopOrderProductDomain;
import com.dadie.Dto.ShopDto;
import com.dadie.Dto.ShopOrderDto;
import com.dadie.Dto.ShopOrderProductDto;
import com.dadie.parame.ShopOrderParame;
import com.damie.mapper.ShopMapper;
import com.damie.mapper.ShopOrderMapper;
import com.damie.mapper.ShopOrderProductMapper;
import com.damie.service.ShopOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.damie.uilt.Copy;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Alon
 * @since 2021-01-24
 */
@Service
public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderMapper, ShopOrderDomain> implements ShopOrderService {

    @Resource
    private ShopOrderMapper mapper;
    @Resource
    private ShopOrderProductMapper smapper;
    @Resource
    private ShopMapper shopMapper;

    @Override
    public Page<ShopOrderDto> selectAllOrder(Integer index, Integer size, ShopOrderParame shopOrderParame) {
        QueryWrapper<ShopOrderDomain> q = new QueryWrapper<>();
        if (shopOrderParame.getValue2()!=null&&shopOrderParame.getValue2().length<1){
            q.ge("create_time",shopOrderParame.getValue2()[0]);

            q.le("create_time",shopOrderParame.getValue2()[1]);
        }
        if (shopOrderParame.getShopOrderStatus()!=null&&shopOrderParame.getShopOrderStatus()!=0){
            q.eq("shop_order_status",shopOrderParame.getShopOrderStatus());
        }
        Page<ShopOrderDomain> shopOrderDomainPage = mapper.selectPage(new Page<ShopOrderDomain>(index, size), q);
        List<ShopOrderDto> collect = shopOrderDomainPage.getRecords().stream().map(shopOrderDomain -> {

            ShopOrderDto copy = (ShopOrderDto) Copy.copy(shopOrderDomain, new ShopOrderDto());

            QueryWrapper<ShopOrderProductDomain> shopOrderProductDomainQueryWrapper = new QueryWrapper<>();

            shopOrderProductDomainQueryWrapper.eq("shop_order_id",copy.getShopOrderId());

            List<ShopOrderProductDomain> shopOrderProductDomains = smapper.selectList(shopOrderProductDomainQueryWrapper);
            //多个商品
            List<ShopOrderProductDto> collect1 = shopOrderProductDomains.stream().map(shopOrderProductDomain -> {
                ShopDomain shopDomain = shopMapper.selectById(shopOrderProductDomain.getProductId());
                ShopOrderProductDto shopOrderProductDto1 = (ShopOrderProductDto)Copy.copy(shopOrderProductDomain, new ShopOrderProductDto());
                shopOrderProductDto1.setShopDto((ShopDto) Copy.copy(shopDomain, new ShopDto()));
                return shopOrderProductDto1;
            }).collect(Collectors.toList());

            copy.setShopOrderProductDto(collect1);

            return copy;
        }).collect(Collectors.toList());
        Page<ShopOrderDto> shopOrderDtoPage = new Page<>();
        BeanUtils.copyProperties(shopOrderDomainPage,shopOrderDtoPage);
        shopOrderDtoPage.setRecords(collect);
        return shopOrderDtoPage;

    }

    @Override
    public Page<ShopOrderDto> selectAllOrder(Integer index, Integer size, ShopOrderParame shopOrderParame, String userId) {
        QueryWrapper<ShopOrderDomain> q = new QueryWrapper<>();
        q.eq("user_id",userId);
        if (shopOrderParame.getShopOrderStatus()!=null&&shopOrderParame.getShopOrderStatus()!=0){
            q.eq("shop_order_status",shopOrderParame.getShopOrderStatus());
        }
        Page<ShopOrderDomain> shopOrderDomainPage = mapper.selectPage(new Page<ShopOrderDomain>(index, size), q);
        List<ShopOrderDto> collect = shopOrderDomainPage.getRecords().stream().map(shopOrderDomain -> {

            ShopOrderDto copy = (ShopOrderDto) Copy.copy(shopOrderDomain, new ShopOrderDto());

            QueryWrapper<ShopOrderProductDomain> shopOrderProductDomainQueryWrapper = new QueryWrapper<>();

            shopOrderProductDomainQueryWrapper.eq("shop_order_id",copy.getShopOrderId());

            List<ShopOrderProductDomain> shopOrderProductDomains = smapper.selectList(shopOrderProductDomainQueryWrapper);
            //多个商品
            List<ShopOrderProductDto> collect1 = shopOrderProductDomains.stream().map(shopOrderProductDomain -> {
                ShopDomain shopDomain = shopMapper.selectById(shopOrderProductDomain.getProductId());
                ShopOrderProductDto shopOrderProductDto1 = (ShopOrderProductDto)Copy.copy(shopOrderProductDomain, new ShopOrderProductDto());
                shopOrderProductDto1.setShopDto((ShopDto) Copy.copy(shopDomain, new ShopDto()));
                return shopOrderProductDto1;
            }).collect(Collectors.toList());

            copy.setShopOrderProductDto(collect1);

            return copy;
        }).collect(Collectors.toList());
        Page<ShopOrderDto> shopOrderDtoPage = new Page<>();
        BeanUtils.copyProperties(shopOrderDomainPage,shopOrderDtoPage);
        shopOrderDtoPage.setRecords(collect);
        return shopOrderDtoPage;

    }
}
